﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Windows.Input;
using Clipping.Infrastructure.Models;
using System.Threading.Tasks;
using System.Windows;
namespace ClippingMaster
{
    public class MainViewModel : GalaSoft.MvvmLight.ViewModelBase
    {


        readonly static SearchEnginesViewModel _searchEngineViewModel = new SearchEnginesViewModel();
        readonly static KeywordsPanelViewModel _keywordsPanelViewModel = new KeywordsPanelViewModel();
        readonly static SearchObjectViewModel _searchObjViewModel = new SearchObjectViewModel();
        private Dictionary<string, ClassInfo> allClientsDic;
        private Log4cb.ILog4cbHelper logHelper = new Log4cb.Log4cbHelper("ClippingMaster");
        public MainViewModel()
        {
            this.BeginSearchCommand = new RelayCommand<Clipping.Infrastructure.Models.SearchObject>(beginSearch);
            _keywordsPanelViewModel.OnKeywordSearch += new Action<Clipping.Infrastructure.Models.KeywordInfo>(OnKeywordSearch);
            _searchObjViewModel.OnKeywordSearch += new Action<KeywordInfo>(OnKeywordSearch);
            Task.Factory.StartNew(deleteExpiredUploadHistory);
            this.allClientsDic = Clipping.Utils.ClientInfoProvider.GetInstance().GetAllClassInfoDic();
         
        }

     

        void deleteExpiredUploadHistory()
        {
            try
            {
                Clipping.Utils.UploadHistoryRepository rep = new Clipping.Utils.UploadHistoryRepository();
                rep.DeleteExpiredRecords(DateTime.Now.AddDays(-30));
            }
            catch (Exception ex)
            {

            }
        }
        
        void OnKeywordSearch(KeywordInfo keyword)
        {
            if (_searchEngineViewModel.SelectedSearchEngine == null || _searchEngineViewModel.SelectedSearchEngine.Count == 0)
            {
                MessageBox.Show("请至少选择一个搜索引擎", "", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }
            PeriodSearchObject so = new PeriodSearchObject(keyword.Keyword, keyword.SearchType, keyword.SearchPeriod);
            beginSearch(so);
        }
       


        private ObservableCollection<Clipping.Infrastructure.Models.News> searchResult;
        public ObservableCollection<Clipping.Infrastructure.Models.News> SearchResult
        {
            get { return searchResult; }
            set
            {
                if (value == searchResult)
                    return;
                searchResult = value;
                base.RaisePropertyChanged(() => this.SearchResult);
            }
        }

        public ICommand BeginSearchCommand;

        public ICommand MakeClippingCommand;
        private void beginSearch(Clipping.Infrastructure.Models.SearchObject searchObj)
        {
            this.SearchSessionVM = new SearchSession(searchObj, _searchEngineViewModel.SelectedSearchEngine.ToList(),this.logHelper);
            this._searchSession.SearchCommand.Execute(null);
        }


        public SearchEnginesViewModel SearchEnginesVM { get { return _searchEngineViewModel; } }

        public KeywordsPanelViewModel KeywordsPanelVM
        {
            get
            {
                return _keywordsPanelViewModel;
            }
        }

        public SearchObjectViewModel SearchObjVM { get { return _searchObjViewModel; } }

        SearchSession _searchSession;
        public SearchSession SearchSessionVM
        {
            get { return _searchSession; }
            set
            {
                this._searchSession = value;
                RaisePropertyChanged(() => this.SearchSessionVM);
            }

        }
    }
}
